package net.server.handler;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import net.client.RequestDispacher;
import net.data.SendNameRequest;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import core.ChatNode;
import core.FindTask;
import core.LoginTask;
import db.Mapper;



public class SendNameHandler implements HttpHandler
{
	private ChatNode node;
	
	public SendNameHandler(ChatNode node)
	{
		this.node = node;
	}
	
	@Override
	public void handle(HttpExchange t) throws IOException
	{
		String remoteAddress = t.getRemoteAddress().getAddress().getHostAddress();
		node.getMainWindow().appendText(remoteAddress + " > " + t.getRequestURI());
		
		String response = "OK";
		t.sendResponseHeaders(200, response.length());
		OutputStream os = t.getResponseBody();
		os.write(response.getBytes());
		os.close();
		
		@SuppressWarnings("unchecked")
		Map<String, String> params = (Map<String, String>)t.getAttribute("parameters");
		
		if(params.containsKey("name") && params.containsKey("ip"))
		{
			String name = params.get("name");
			String address = params.get("ip");
			
			Mapper userbase = node.getUserBase();
			
			if(userbase != null)
			{
				LoginTask loginTask = node.getLoginTask();
				if(loginTask != null)
				{
					if(loginTask.isDone())
					{
						String myName = node.getName();
						String myAddress = node.getIp() + ":" + node.getPort();
						if(!name.equals(myName))
						{
							if(!address.equals(myAddress))
							{
								if(!userbase.containsAddress(address))
								{
									userbase.removePeer(name);
								}
								
								//findName
								FindTask findTask = node.getFindTask();
								if(findTask != null)
								{
									if(!findTask.isDone())
									{
										String findName = findTask.getFindName();
										if(name.equals(findName))
										{
											node.getMainWindow().appendText("Peer \"" + name + "\" found: "+ address);
											findTask.cancel(true);
										}
									}
								}
									
								userbase.addPeer(name, address);
								node.getMainWindow().setUserList(userbase.getPeersArray());
								node.getMainWindow().appendText("INFO: New contact " + name + "@" + address);
							}
							else
							{
								//kontakti andmebaasi uuendamine, tal on juba minu aadress olemas aga vananenud nimi
								SendNameRequest request = new SendNameRequest();
								request.setToAddress(remoteAddress);
								request.setAddress(myAddress);
								request.setName(myName);
								request.setTtl(1);
								
								RequestDispacher dispacher = new RequestDispacher(node);
								dispacher.dispach(request);
							}
						}
					}
					else
					{
						//sisselogimine poole peal
						String myName = loginTask.getLoginName();
						String myAddress = node.getIp() + ":" + node.getPort();
						
						if(address.equals(myAddress) && !name.equals(myName))
						{
							SendNameRequest request = new SendNameRequest();
							request.setToAddress(remoteAddress);
							request.setAddress(myAddress);
							request.setName(myName);
							request.setTtl(1);
							
							RequestDispacher dispacher = new RequestDispacher(node);
							dispacher.dispach(request);
						}
						else if(!address.equals(myAddress) && name.equals(myName))
						{
							loginTask.cancel(true);
							node.getMainWindow().appendText("Login cancelled!");
						}
					}
				}
			}
		}
	}
}
